package com.lyj.io.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyj.io.common.utils.DateUtil;
import com.lyj.io.common.utils.PageUtils;
import com.lyj.io.common.utils.Query;
import com.lyj.io.module.sys.dao.SysOrgDao;
import com.lyj.io.module.sys.entity.SysOrg;
import com.lyj.io.module.sys.service.SysOrgService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 部门接口
 * @author: 李亚杰
 * @version: 1.0
 * @date: 2021/8/19 14:31
 */
@Service
public class SysOrgServiceImpl extends ServiceImpl<SysOrgDao, SysOrg> implements SysOrgService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SysOrg> queryWrapper = new QueryWrapper<SysOrg>();
        queryWrapper.eq("delete_flag","0");
        queryWrapper.eq("org_type","0");
        queryWrapper.eq("parent_org","0");
        IPage<SysOrg> page = this.page(
                new Query<SysOrg>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void removeByIds(List<String> departmentIds){
        UpdateWrapper<SysOrg> updateWrapper = new UpdateWrapper<SysOrg>();
        updateWrapper.in("org_id",departmentIds);
        updateWrapper.set("delete_flag", DateUtil.format2String(DateUtil.YYYYMMDDHHMMSS, new Date()));
        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public void changeOrgStatus(SysOrg sysOrg) {
        UpdateWrapper<SysOrg> updateWrapper = new UpdateWrapper<SysOrg>();
        updateWrapper.in("org_id", sysOrg.getOrgId());
        updateWrapper.set("status", sysOrg.getStatus());
        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public List<SysOrg> getChildrenOrg(String parentOrg) {
        QueryWrapper<SysOrg> queryWrapper = new QueryWrapper<SysOrg>();
        queryWrapper.eq("delete_flag", "0");
        queryWrapper.eq("parent_org", parentOrg);
        this.baseMapper.selectList(queryWrapper);
        return this.baseMapper.selectList(queryWrapper);
    }
}
